//
//  ACWAccountsTextField.m
//  pickerView
//
//  Created by rylynn lai on 15/10/12.
//  Copyright © 2015年 rylynn lai. All rights reserved.
//

#import "ACWAccountsTextField.h"
#import "ACWAccountsView.h"
#import "ACWAccount.h"

@interface ACWAccountsTextField ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) NSArray *accounts;

@property (nonatomic) BOOL initial;
@end

@implementation ACWAccountsTextField

- (NSArray *)accounts {
    if (!_accounts) {
        _accounts = [ACWAccount accountList];
    }
    return _accounts;
}

//当外部用init这类方法创建时,保证能初始化正确.
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}
//当外部用SB或者xib创建时,保证初始化正确
- (void)awakeFromNib {
    [self setUp];
}

- (void)setUp {
    UIPickerView *accountView = [[UIPickerView alloc] init];
    //自定义输入窗口
    self.inputView = accountView;
    accountView.delegate = self;
    accountView.dataSource = self;
    [self initialTextField];
}

- (void)initialTextField{
//    [self.flagView selectRow:0 inComponent:0 animated:YES];//不是这个方法
    if (!_initial) {
        [self pickerView:(UIPickerView *)self.inputView didSelectRow:0 inComponent:0];
        _initial = YES;
    }
}

#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.accounts.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    ACWAccountsView *flagView = [ACWAccountsView accountsView];
    flagView.account = self.accounts[row];
    return flagView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 80;
}

#pragma mark - UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.text = [NSString stringWithFormat:@"%@: %@", [self.accounts[row] accountType], [self.accounts[row] account]];
}

@end
